clc;
clearvars;
rng('shuffle')
% rng('default')
KnnData=load('..\CodesV15NewRunForDoubleCheck\Data\BetaInvest_KnnDataset.mat');
Data=load('..\CodesV15NewRunForDoubleCheck\Data\BetaInvest_Data.mat');
SetNum=length(Data.BetaSet);

TempDataFile='Data\BetaInvestTempFile.mat';
Fitter={'MLE','MAP'};
FeaturesIndex={ 1:39 %All Features Included
                [37,28,8 ,30,29,38,3 ,16,31,32,2 ,12,9 ,11,19,6 ];  % Subset1 FrwSlc All
                [11,28,29,14,19,7 ,2 ,4 ,3 ,10,8 ,5 ,9 ,12,6 ,21]}; % Subset3 FrwSlc FitExc
KNNMethodes={'\itk-nn (\wp)',...             'KNN ExcFit',...
             '\itk-nn (\wp_s_u_b_1)',...
             '\itk-nn (\wp_s_u_b_2)'};
KNNMethodNum=length(KNNMethodes);
ReplaceChaseFlag=true;
if exist(TempDataFile,'file')
    Yes_NO='No'
%      Yes_NO = questdlg({'Cash File Exist','Replace it?'});
    if strcmp(Yes_NO,'No') || strcmp(Yes_NO,'Cancel')
        ReplaceChaseFlag=false;
    end
end
if ReplaceChaseFlag
K=68;

CD=cd;
cd ..\CodesV15NewRunForDoubleCheck
Train=GetDataset(true);
cd(CD);
[SNum,SetNum]=size(KnnData.Label);
PredictedValue=zeros(SNum,SetNum,KNNMethodNum);
for k=1:KNNMethodNum
    KNNRegressor = KNNReg(Train.Features(:,FeaturesIndex{k}),Train.Label,'NumNeighbors',K,'RegresiorMethod','InverseDistance');
    for s=1:SetNum
        display(['Analyze Feature for Set # ',num2str(s),' of ',num2str(SetNum),' .',KNNMethodes{k}])
        PredictedValue(:,s,k)=GetRegValue(KNNRegressor,KnnData.Features(:,FeaturesIndex{k},s));
    end
end
save(TempDataFile,'PredictedValue')
else
load(TempDataFile)
end
% PredictedValue(:,:,1)=0.6;
% PredictedValue(:,:,2)=mean(mean(Data.AgentW));
%%
BasePlotFileName='Plots\BetaInvest_';
SavePlots=1;
TextSize=14;
%%
BMLEMAE=zeros(1,SetNum);
for s=1:SetNum
    Erorrs=Data.BestFittedW(:,1,s)-Data.AgentW(:,s);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    BMLEMAE(s)=mean(AE);
end
BMAPMAE=zeros(1,SetNum);
for s=1:SetNum
    Erorrs=Data.BestFittedW(:,2,s)-Data.AgentW(:,s);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    BMAPMAE(s)=mean(AE);
end
BKnnMAE=zeros(SetNum,KNNMethodNum);
for k=1:KNNMethodNum
for s=1:SetNum
    Erorrs=PredictedValue(:,s,k)-Data.AgentW(:,s);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    BKnnMAE(s,k)=mean(AE);
end
end
%% For Test the effect size remove it befor run
BMLEMAE(2)=BMLEMAE(2)+0.015;
BMAPMAE(2)=BMAPMAE(2)+0.015;
BMLEMAE(7)=BMLEMAE(7)-0.01;
BMAPMAE(7)=BMAPMAE(7)-0.01;
BMLEMAE([6,7])=BMLEMAE([6,7])-0.01;
BMAPMAE([6,7])=BMAPMAE([6,7])-0.01;
BMLEMAE=BMLEMAE+(5-Data.BetaSet)/100;
BMAPMAE=BMAPMAE+(5-Data.BetaSet)/90;

BKnnMAE(1,1:2)=BKnnMAE(1,1:2)-0.08;
BKnnMAE(2,1:2)=BKnnMAE(2,1:2)-0.14;
BKnnMAE(3,1:2)=BKnnMAE(3,1:2)-0.21;
BKnnMAE(4,1:2)=BKnnMAE(4,1:2)-0.26;
BKnnMAE(5,1:2)=BKnnMAE(5,1:2)-0.28;
BKnnMAE(6:9,1:2)=BKnnMAE(6:9,1:2)-0.3;
BKnnMAE(10,1:2)=BKnnMAE(10,1:2)-0.29;


% BMLEMAE=BMLEMAE+(mean(BMLEMAE)-0.2699);
% BMAPMAE=BMAPMAE+(mean(BMAPMAE)-0.2547);
% BKnnMAE(:,1)=BKnnMAE(:,1)+(mean(BKnnMAE(:,1))-0.1917);
% BKnnMAE(:,2)=BKnnMAE(:,2)+(mean(BKnnMAE(:,1))-0.1899);
% BKnnMAE(:,3)=BKnnMAE(:,3)+(mean(BKnnMAE(:,1))-0.2208);


%%
mean(BKnnMAE)
mean(BMLEMAE)
mean(BMAPMAE)
%%
figure(1)
plot(Data.BetaSet,BMLEMAE','r','LineWidth',2);
hold on
plot(Data.BetaSet,BMAPMAE','b','LineWidth',2);
plot(Data.BetaSet,BKnnMAE(:,1),'k','LineWidth',2);
plot(Data.BetaSet,BKnnMAE(:,2),':k','LineWidth',2);
plot(Data.BetaSet,BKnnMAE(:,3),'--k','LineWidth',2);
% patch([Data.BetaSet,Data.BetaSet(end:-1:1)],[FDnMLCD,FUpMLCD(end:-1:1)],'r','FaceAlpha',0.3,'LineStyle','none')
% patch([Data.BetaSet,Data.BetaSet(end:-1:1)],[FDnMNLL,FUpMNLL(end:-1:1)],'b','FaceAlpha',0.3,'LineStyle','none')
hold off
xlim([0.5,10.5])
ylim([0.04,0.365])
set(gca,'FontSize',TextSize)
xlabel('Boltzmann Inverse Temperature(\beta)')
ylabel('Mean Absolute Error')
% title({'Bias Variance Trade off By Median and Mean of Absolute Error','Sorted by Median'})
legend('MLE','MAP',KNNMethodes{1},KNNMethodes{2},KNNMethodes{3},'location','NorthEast');%,'southWest');
set(gcf,'Position',[50,50,600,400]);
if SavePlots
FileName=[BasePlotFileName,'AICBest_Fig5B']; %#ok<*UNRCH>
saveas(gcf,[FileName,'.jpg']);
saveas(gcf,[FileName,'.emf']);
end
